package com.ev.custom.service.impl;

import com.ev.custom.vo.DevicePatrolSaveVO;
import com.ev.framework.il8n.MessageSourceHandler;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ev.custom.dao.DevicePatrolDao;
import com.ev.custom.domain.DevicePatrolDO;
import com.ev.custom.service.DevicePatrolService;
import com.ev.custom.vo.DevicePatrolPageParam;
import com.ev.framework.utils.R;
import com.ev.apis.model.DsResultResponse;
import com.google.common.collect.Maps;


@Service
public class DevicePatrolServiceImpl implements DevicePatrolService {
    @Autowired
    private DevicePatrolDao devicePatrolDao;
    @Autowired
    private MessageSourceHandler messageSourceHandler;

    @Override
    public DevicePatrolDO get(Long id) {
        return devicePatrolDao.get(id);
    }

    @Override
    public List<DevicePatrolDO> list(Map<String, Object> map) {
        return devicePatrolDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return devicePatrolDao.count(map);
    }

    @Override
    public int save(DevicePatrolDO devicePatrol) {
        return devicePatrolDao.save(devicePatrol);
    }

    @Override
    public int update(DevicePatrolDO devicePatrol) {
        return devicePatrolDao.update(devicePatrol);
    }

    @Override
    public int updateAll(DevicePatrolDO devicePatrol) {
        return devicePatrolDao.updateAll(devicePatrol);
    }

    @Override
    public int remove(Long id) {
        return devicePatrolDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return devicePatrolDao.batchRemove(ids);
    }

    @Override
    public R saveAndVerify(DevicePatrolSaveVO saveParam) {
        Long deviceId = saveParam.getDeviceId();
        List<Long> patrolProjectIds = saveParam.getPatrolProjectIds();
        if (deviceId == null || patrolProjectIds == null || patrolProjectIds.size() == 0) {
            return R.error();
        }
        DevicePatrolPageParam devicePatrolPageParam = new DevicePatrolPageParam();
        devicePatrolPageParam.setPatrolProjectIds(patrolProjectIds);
        devicePatrolPageParam.setDeviceId(deviceId);
        devicePatrolPageParam.setPagesize(Integer.MAX_VALUE);
        List<Map<String, Object>> projectList = devicePatrolDao.pageList(devicePatrolPageParam);
        if (projectList.size()>0) {
            String patrolProjectName = projectList
                    .stream()
                    .map(e -> e.get("patrolProjectName").toString())
                    .collect(Collectors.joining(","));
            String[] args = {patrolProjectName};
            return R.error(messageSourceHandler.getMessage("device.project.isExist", args));
        }

        List<DevicePatrolDO> devicePatrolDOList = Lists.newArrayList();
        DevicePatrolDO devicePatrolDO;
        for (Long patrolProjectId : patrolProjectIds) {
            devicePatrolDO = new DevicePatrolDO();
            devicePatrolDO.setPatrolProjectId(patrolProjectId);
            devicePatrolDO.setDeviceId(deviceId);
            devicePatrolDOList.add(devicePatrolDO);
        }
        devicePatrolDao.batchSave(devicePatrolDOList);
        return R.ok();
    }

    @Override
    public Map<String, Object> getById(Long id) {
        DevicePatrolDO devicePatrol = devicePatrolDao.get(id);
        Map<String, Object> result = Maps.newHashMap();
        if (devicePatrol !=null){
            result.put("data",devicePatrol);
            return result;
        }
        return result;
    }

    @Override
    public Map<String, Object> pageList(DevicePatrolPageParam pageParam) {
        Map<String, Object> result = Maps.newHashMap();
        List<Map<String, Object>> data = devicePatrolDao.pageList(pageParam);
        if (data.size() > 0) {
            int total = devicePatrolDao.pageCount(pageParam);
            result.put("data", new DsResultResponse(pageParam.getPageno(), pageParam.getPagesize(), total, data));
        }
        return result;
    }

}
